package com.molyfun.parents.activity.classroom;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.molyfun.parents.MFPApplication;
import com.molyfun.parents.R;
import com.molyfun.parents.activity.common.BaseChoosePicActivity;
import com.molyfun.parents.activity.common.attachment.AttachmentView;
import com.molyfun.parents.fragment.dialog.CommonDialogFragment;
import com.molyfun.parents.rest.bean.MediaobjectlistBean;
import com.molyfun.parents.rest.bean.NewSignUpSubmitInfoRequest;
import com.molyfun.parents.rest.bean.NewTeamEvaluateTrainingListResponse;
import com.molyfun.parents.rest.bean.NewTeamSheetListResponse;
import com.molyfun.parents.rest.bean.SignUpSubmitInfoResponse;
import com.molyfun.parents.uievent.EventDispatcherEnum;
import com.molyfun.parents.utils.Const;

import java.util.ArrayList;

import butterknife.ButterKnife;
import butterknife.InjectView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class NewTeamSheetActivity extends BaseChoosePicActivity implements View.OnClickListener, AttachmentView.AttachmentAddCallback {

    private String TAG = "SignUpEvalucateSheetActivity";
    @InjectView(R.id.base_title_left)
    ImageView mBack;
    @InjectView(R.id.base_title)
    TextView mTitle;
    @InjectView(R.id.recyclerView)
    RecyclerView mRecyclerView;

    @InjectView(R.id.submit_button)
    TextView mSubmitButton;
    private NewTeamSheetAdapter newTeamSheetAdapter;

    private String mGroupId;

    private ArrayList<NewTeamEvaluateTrainingListResponse.NewTeamEvaluateBean> mNewTeamTrainingBean;

    public static final void startActivity(Context context, String group_id, ArrayList<NewTeamEvaluateTrainingListResponse.NewTeamEvaluateBean> trainingBean) {
        Intent intent = new Intent();
        intent.setClass(context, NewTeamSheetActivity.class);
        intent.putExtra(Const.GROUP_ID, group_id);
        intent.putParcelableArrayListExtra(Const.NEW_TEAM_TRAINING_BEAN_LIST, trainingBean);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_team_sheet);
        ButterKnife.inject(this);
        initView();

    }

    private void initView() {
        getSupportActionBar().setTitle("");
        mTitle.setText("分组评测表");
        mBack.setOnClickListener(this);
        mSubmitButton.setOnClickListener(this);

        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        newTeamSheetAdapter = new NewTeamSheetAdapter(this, this, getSupportFragmentManager());
        mRecyclerView.setAdapter(newTeamSheetAdapter);

        mGroupId = getIntent().getStringExtra(Const.GROUP_ID);
        mNewTeamTrainingBean = getIntent().getParcelableArrayListExtra(Const.NEW_TEAM_TRAINING_BEAN_LIST);
        if(mNewTeamTrainingBean != null){
            System.out.println("mNewTeamTrainingBean---" + mNewTeamTrainingBean.size());
        }

        if (!TextUtils.isEmpty(mGroupId)) {
            loadData();
        } else {
            finish();
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.base_title_left:
                finish();
                break;

            case R.id.submit_button:
                submitData();
                break;
        }
    }

    @Override
    public void onAttachmentAdd() {
        chooseMedia();
    }

    private void loadData() {
        showProgressDialog();
        Call<NewTeamSheetListResponse> call = MFPApplication.getInstance().getRestClient().getClassTeamEvaluateList(mGroupId);
        call.enqueue(new Callback<NewTeamSheetListResponse>() {
            @Override
            public void onResponse(Call<NewTeamSheetListResponse> call, Response<NewTeamSheetListResponse> response) {
                dismissProgressDialog();
                if (response.body() != null && response.body().getData() != null) {
                    newTeamSheetAdapter.refreshData(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<NewTeamSheetListResponse> call, Throwable t) {
                dismissProgressDialog();
            }
        });
    }

    private void submitData() {
        NewSignUpSubmitInfoRequest newSignUpSubmitInfoRequest = new NewSignUpSubmitInfoRequest();
        newSignUpSubmitInfoRequest.setGroupid(mGroupId);
        newSignUpSubmitInfoRequest.setTraining(mNewTeamTrainingBean);
        newSignUpSubmitInfoRequest.setResultlist(newTeamSheetAdapter.getInputResult());
        showProgressDialog();
        Call<SignUpSubmitInfoResponse> submitInfoCall = MFPApplication.getInstance().getRestClient().newTeamSheetSubmitInfo(newSignUpSubmitInfoRequest);
        submitInfoCall.enqueue(new Callback<SignUpSubmitInfoResponse>() {
            @Override
            public void onResponse(Call<SignUpSubmitInfoResponse> call, Response<SignUpSubmitInfoResponse> response) {
                dismissProgressDialog();
                if (response.body() != null) {
                    if (response.body().getCode() == 200) {
                        new CommonDialogFragment.CommonDialogBuilder("提交成功", "报名成功请等待老师审核，审核通过后可在我的班级中查看分组情况。")
                                .setCommonDialogCallback(new CommonDialogFragment.CommonDialogCallback() {
                                    @Override
                                    public void onCommonDialogClose() {
                                        finish();
                                        Message message = new Message();
                                        message.what = EventDispatcherEnum.UI_EVENT_NEW_TEAM_SHEET_FINISH;
                                        MFPApplication.getInstance().getEventDispatcher().handleMessage(message);
                                    }
                                })
                                .build(getSupportFragmentManager().beginTransaction());
                    } else {
                        toast(response.body().getMsg());
                    }
                }else{
                    toast("提交失败");
                }

            }

            @Override
            public void onFailure(Call<SignUpSubmitInfoResponse> call, Throwable t) {
                dismissProgressDialog();
                toast("提交失败");
            }
        });
    }

    @Override
    public String getAttachmentFolder() {
        return "teamSheet";
    }

    @Override
    public void onPictureUploadBegin(String path, String UploadPath) {

    }

    @Override
    public void onPictureUploadSuccess(String localPath, String smallUrl, String originalUrl) {
        if(!TextUtils.isEmpty(originalUrl)){
            MediaobjectlistBean mediaobjectlistBean = new MediaobjectlistBean();
            mediaobjectlistBean.setUrl(originalUrl);
            mediaobjectlistBean.setType(1);
            newTeamSheetAdapter.addAttachment(mediaobjectlistBean);
        }
    }

    @Override
    public void onPictureUploadFailed(String path, String UploadPath) {

    }

    @Override
    public void onVideoUploadSuccess(String cover, String path) {
        if(!TextUtils.isEmpty(path)){
            MediaobjectlistBean mediaobjectlistBean = new MediaobjectlistBean();
            mediaobjectlistBean.setUrl(path);
            mediaobjectlistBean.setCover(cover);
            mediaobjectlistBean.setType(2);
            newTeamSheetAdapter.addAttachment(mediaobjectlistBean);
        }
    }
}

